﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Repository;
using Ext.Net;
using ExaminationSys.Helper;
namespace ExaminationSys.View.Users
{
    public partial class SysUserList : System.Web.UI.Page
    {
        private static readonly Sys_UserService Proxy = ObjectFactory<Sys_UserService>.Create();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Page_Init(object sender, EventArgs e)
        {
            this.EditWindow.GridStore = this.StoreEntity;
        }

        private int pageSize = 20;
        protected void StoreEntity_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            int pageIndex = (e.Start / e.Limit) + 1;
            int totalPage, totalRecord;
            this.StoreEntity.DataSource = Proxy.GetListByPage(" 1=1 ", " OperatorTime desc", pageSize, pageIndex,
                                                                   out totalPage, out totalRecord);// Employee.GetEmployeesFilter(e.Start, e.Limit, e.Sort.Length > 0 ? e.Sort[0] : null, out count);
            e.Total = totalRecord;

            this.StoreEntity.DataBind();
        }

        protected void Command(object sender, DirectEventArgs e)
        {
            var command = e.ExtraParams["command"];

            var id = e.ExtraParams["Id"];
            if (string.IsNullOrWhiteSpace(id))
            {
                MessageBoxHelper.ShowInfo("获取ID失败");
                return;
            }

            if (command.Equals("edit", StringComparison.OrdinalIgnoreCase))
            {
                ShowDetails(id);
            }
            else
            {
                var entity = Proxy.Get(new Guid(id));
                if (entity != null)
                {
                    entity.DeleteKey = 0;
                    var success = Proxy.Update(entity);
                    if (success)
                    {
                        StoreEntity.Remove(entity.UserID);
                        MessageBoxHelper.ShowInfo("删除成功");
                    }
                }
            }
        }

        protected void Add_Click(object sender, DirectEventArgs e)
        {
            this.EditWindow.SetData(null);
            this.EditWindow.Show();
        }

        protected void ShowDetails(string id)
        {
            var realId = new Guid(id);
            var result = Proxy.Get(realId);
            if (result != null)
            {
                EditWindow.GridStore = StoreEntity;
                this.EditWindow.SetData(result);
                this.EditWindow.Show();
            }
        }
    }
}